<html>
<head><meta charset="utf-8"><title>split array to consecutive subarray ? · general · Zulip Chat Archive</title></head>
<h2>Stream: <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/index.html">general</a></h2>
<h3>Topic: <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/split.20array.20to.20consecutive.20subarray.20.3F.html">split array to consecutive subarray ?</a></h3>

<hr>

<base href="https://rust-lang.zulipchat.com">

<head><link href="https://rust-lang.github.io/zulip_archive/style.css" rel="stylesheet"></head>

<a name="212219104"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/split%20array%20to%20consecutive%20subarray%20%3F/near/212219104" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> lzutao <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/split.20array.20to.20consecutive.20subarray.20.3F.html#212219104">(Oct 04 2020 at 11:31)</a>:</h4>
<p>Is there a way to split array const of Copy type to consecutive sub-array ?<br>
For example, I have a <code>[u8; 32]</code>const, I want to split it into <code>[u8; 8]</code> and <code>[u8; 24]</code>.</p>



<a name="212219159"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/split%20array%20to%20consecutive%20subarray%20%3F/near/212219159" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> lzutao <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/split.20array.20to.20consecutive.20subarray.20.3F.html#212219159">(Oct 04 2020 at 11:32)</a>:</h4>
<p>cc <span class="user-mention" data-user-id="120791">@RalfJ</span> if you don't mind.</p>



<a name="212219236"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/split%20array%20to%20consecutive%20subarray%20%3F/near/212219236" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> lcnr <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/split.20array.20to.20consecutive.20subarray.20.3F.html#212219236">(Oct 04 2020 at 11:35)</a>:</h4>
<p>what exactly do you need this for?</p>



<a name="212219309"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/split%20array%20to%20consecutive%20subarray%20%3F/near/212219309" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/split.20array.20to.20consecutive.20subarray.20.3F.html#212219309">(Oct 04 2020 at 11:37)</a>:</h4>
<p><span class="user-mention silent" data-user-id="209117">lzutao</span> <a href="#narrow/stream/122651-general/topic/split.20array.20to.20consecutive.20subarray.20.3F/near/212219159">said</a>:</p>
<blockquote>
<p>cc <span class="user-mention silent" data-user-id="120791">RalfJ</span> if you don't mind.</p>
</blockquote>
<p>sorry, I am not involved in the const generic project and cannot help with such questions</p>



<a name="212219324"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/split%20array%20to%20consecutive%20subarray%20%3F/near/212219324" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> lcnr <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/split.20array.20to.20consecutive.20subarray.20.3F.html#212219324">(Oct 04 2020 at 11:37)</a>:</h4>
<p>if you only need <code>&amp;[u8; 8]</code> and <code>&amp;[u8; 24]</code> you can use</p>
<div class="codehilite" data-code-language="Rust"><pre><span></span><code><span class="kd">let</span><span class="w"> </span><span class="p">(</span><span class="n">head</span><span class="p">,</span><span class="w"> </span><span class="n">tail</span><span class="p">)</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">arr</span><span class="p">.</span><span class="n">split_at</span><span class="p">(</span><span class="mi">8</span><span class="p">).</span><span class="n">unwrap</span><span class="p">();</span><span class="w"></span>
<span class="kd">let</span><span class="w"> </span><span class="n">fst</span><span class="w"> </span>: <span class="kp">&amp;</span><span class="p">[</span><span class="kt">u8</span><span class="p">;</span><span class="w"> </span><span class="mi">8</span><span class="p">]</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">head</span><span class="p">.</span><span class="n">try_into</span><span class="p">().</span><span class="n">unwrap</span><span class="p">();</span><span class="w"></span>
<span class="kd">let</span><span class="w"> </span><span class="n">snd</span>: <span class="kp">&amp;</span><span class="p">[</span><span class="kt">u8</span><span class="p">;</span><span class="w"> </span><span class="mi">24</span><span class="p">]</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">tail</span><span class="p">.</span><span class="n">try_into</span><span class="p">().</span><span class="n">unwrap</span><span class="p">();</span><span class="w"></span>
</code></pre></div>



<a name="212219368"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/split%20array%20to%20consecutive%20subarray%20%3F/near/212219368" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> lcnr <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/split.20array.20to.20consecutive.20subarray.20.3F.html#212219368">(Oct 04 2020 at 11:38)</a>:</h4>
<p>the try_into should get optimized away by llvm</p>



<a name="212219378"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/split%20array%20to%20consecutive%20subarray%20%3F/near/212219378" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> lcnr <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/split.20array.20to.20consecutive.20subarray.20.3F.html#212219378">(Oct 04 2020 at 11:39)</a>:</h4>
<p>if by value, something like this would work</p>
<div class="codehilite" data-code-language="Rust"><pre><span></span><code><span class="cp">#![feature(const_generics, const_evaluatable_checked)]</span><span class="w"></span>
<span class="cp">#![allow(incomplete_features)]</span><span class="w"></span>

<span class="k">use</span><span class="w"> </span><span class="n">std</span>::<span class="p">{</span><span class="n">mem</span><span class="p">,</span><span class="w"> </span><span class="n">ptr</span><span class="p">};</span><span class="w"></span>

<span class="k">fn</span> <span class="nf">split_first</span><span class="o">&lt;</span><span class="n">T</span><span class="p">,</span><span class="w"> </span><span class="k">const</span><span class="w"> </span><span class="n">N</span>: <span class="kt">usize</span><span class="o">&gt;</span><span class="p">(</span><span class="n">arr</span>: <span class="p">[</span><span class="n">T</span><span class="p">;</span><span class="w"> </span><span class="n">N</span><span class="p">])</span><span class="w"> </span>-&gt; <span class="p">(</span><span class="n">T</span><span class="p">,</span><span class="w"> </span><span class="p">[</span><span class="n">T</span><span class="p">;</span><span class="w"> </span><span class="n">N</span><span class="w"> </span><span class="o">-</span><span class="w"> </span><span class="mi">1</span><span class="p">])</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">    </span><span class="kd">let</span><span class="w"> </span><span class="n">arr</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">mem</span>::<span class="n">ManuallyDrop</span>::<span class="n">new</span><span class="p">(</span><span class="n">arr</span><span class="p">);</span><span class="w"></span>
<span class="w">    </span><span class="k">unsafe</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">        </span><span class="kd">let</span><span class="w"> </span><span class="n">head</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">ptr</span>::<span class="n">read</span><span class="p">(</span><span class="o">&amp;</span><span class="n">arr</span><span class="p">[</span><span class="mi">0</span><span class="p">]);</span><span class="w"></span>
<span class="w">        </span><span class="kd">let</span><span class="w"> </span><span class="n">tail</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">ptr</span>::<span class="n">read</span><span class="p">(</span><span class="o">&amp;</span><span class="n">arr</span><span class="p">[</span><span class="mi">1</span><span class="o">..</span><span class="p">]</span><span class="w"> </span><span class="k">as</span><span class="w"> </span><span class="o">*</span><span class="k">const</span><span class="w"> </span><span class="p">[</span><span class="n">T</span><span class="p">]</span><span class="w"> </span><span class="k">as</span><span class="w"> </span><span class="o">*</span><span class="k">const</span><span class="w"> </span><span class="p">[</span><span class="n">T</span><span class="p">;</span><span class="w"> </span><span class="n">N</span><span class="w"> </span><span class="o">-</span><span class="w"> </span><span class="mi">1</span><span class="p">]);</span><span class="w"></span>
<span class="w">        </span><span class="p">(</span><span class="n">head</span><span class="p">,</span><span class="w"> </span><span class="n">tail</span><span class="p">)</span><span class="w"></span>
<span class="w">    </span><span class="p">}</span><span class="w"></span>
<span class="p">}</span><span class="w"></span>

<span class="k">fn</span> <span class="nf">main</span><span class="p">()</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">    </span><span class="kd">let</span><span class="w"> </span><span class="n">arr</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="p">[</span><span class="mi">0</span><span class="p">,</span><span class="w"> </span><span class="mi">1</span><span class="p">,</span><span class="w"> </span><span class="mi">2</span><span class="p">,</span><span class="w"> </span><span class="mi">3</span><span class="p">,</span><span class="w"> </span><span class="mi">4</span><span class="p">];</span><span class="w"></span>
<span class="w">    </span><span class="kd">let</span><span class="w"> </span><span class="p">(</span><span class="n">head</span><span class="p">,</span><span class="w"> </span><span class="n">tail</span><span class="p">)</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">split_first</span><span class="p">(</span><span class="n">arr</span><span class="p">);</span><span class="w"></span>
<span class="w">    </span><span class="n">assert_eq</span><span class="o">!</span><span class="p">(</span><span class="n">head</span><span class="p">,</span><span class="w"> </span><span class="mi">0</span><span class="p">);</span><span class="w"></span>
<span class="w">    </span><span class="n">assert_eq</span><span class="o">!</span><span class="p">(</span><span class="n">tail</span><span class="p">,</span><span class="w"> </span><span class="p">[</span><span class="mi">1</span><span class="p">,</span><span class="w"> </span><span class="mi">2</span><span class="p">,</span><span class="w"> </span><span class="mi">3</span><span class="p">,</span><span class="w"> </span><span class="mi">4</span><span class="p">]);</span><span class="w"></span>
<span class="p">}</span><span class="w"></span>
</code></pre></div>



<a name="212219387"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/split%20array%20to%20consecutive%20subarray%20%3F/near/212219387" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> lzutao <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/split.20array.20to.20consecutive.20subarray.20.3F.html#212219387">(Oct 04 2020 at 11:39)</a>:</h4>
<p>hashing. It's not entirely needed, but I want to define 4 u64 constant<br>
from a const <code>[u8; 32]</code>.</p>



<a name="212219443"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/split%20array%20to%20consecutive%20subarray%20%3F/near/212219443" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> lcnr <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/split.20array.20to.20consecutive.20subarray.20.3F.html#212219443">(Oct 04 2020 at 11:40)</a>:</h4>
<p>then probably <code>*arr[0..8].try_into().unwrap()</code></p>



<a name="212219470"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/split%20array%20to%20consecutive%20subarray%20%3F/near/212219470" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> lzutao <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/split.20array.20to.20consecutive.20subarray.20.3F.html#212219470">(Oct 04 2020 at 11:41)</a>:</h4>
<p>it works, but not in const context since traits aren't allowed.</p>



<a name="212219596"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/split%20array%20to%20consecutive%20subarray%20%3F/near/212219596" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> lcnr <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/split.20array.20to.20consecutive.20subarray.20.3F.html#212219596">(Oct 04 2020 at 11:44)</a>:</h4>
<p>does</p>
<div class="codehilite" data-code-language="Rust"><pre><span></span><code><span class="k">unsafe</span><span class="w"> </span><span class="p">{</span><span class="w"> </span><span class="o">*</span><span class="p">(</span><span class="o">&amp;</span><span class="n">arr</span><span class="p">[</span><span class="mi">0</span><span class="o">..</span><span class="mi">8</span><span class="p">]</span><span class="w"> </span><span class="k">as</span><span class="w"> </span><span class="o">*</span><span class="k">const</span><span class="p">[</span><span class="kt">u8</span><span class="p">]</span><span class="w"> </span><span class="k">as</span><span class="w"> </span><span class="o">*</span><span class="k">const</span><span class="w"> </span><span class="p">[</span><span class="kt">u8</span><span class="p">;</span><span class="w"> </span><span class="mi">8</span><span class="p">])</span><span class="w"> </span><span class="p">}</span><span class="w"></span>
</code></pre></div>

<p>work?</p>



<a name="212219829"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/split%20array%20to%20consecutive%20subarray%20%3F/near/212219829" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> lzutao <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/split.20array.20to.20consecutive.20subarray.20.3F.html#212219829">(Oct 04 2020 at 11:51)</a>:</h4>
<div class="codehilite" data-code-language="Rust"><pre><span></span><code><span class="n">error</span><span class="p">[</span><span class="n">E0015</span><span class="p">]</span>: <span class="nc">calls</span><span class="w"> </span><span class="k">in</span><span class="w"> </span><span class="n">constants</span><span class="w"> </span><span class="n">are</span><span class="w"> </span><span class="n">limited</span><span class="w"> </span><span class="n">to</span><span class="w"> </span><span class="n">constant</span><span class="w"> </span><span class="n">functions</span><span class="p">,</span><span class="w"> </span><span class="n">tuple</span><span class="w"> </span><span class="n">structs</span><span class="w"> </span><span class="n">and</span><span class="w"> </span><span class="n">tuple</span><span class="w"> </span><span class="n">variants</span><span class="w"></span>
<span class="w">  </span><span class="o">-</span>-&gt; <span class="nc">src</span><span class="o">/</span><span class="n">sip</span><span class="o">/</span><span class="n">hasher</span><span class="p">.</span><span class="n">rs</span>:<span class="mi">18</span>:<span class="mi">43</span><span class="w"></span>
<span class="w">   </span><span class="o">|</span><span class="w"></span>
<span class="mi">18</span><span class="w"> </span><span class="o">|</span><span class="w"> </span><span class="k">const</span><span class="w"> </span><span class="n">A</span>: <span class="kt">u64</span> <span class="o">=</span><span class="w"> </span><span class="k">unsafe</span><span class="w"> </span><span class="p">{</span><span class="w"> </span><span class="n">mem</span>::<span class="n">transmute</span><span class="p">(</span><span class="o">*</span><span class="p">(</span><span class="o">&amp;</span><span class="n">INIT_STATE</span><span class="p">[</span><span class="o">..</span><span class="mi">8</span><span class="p">]</span><span class="w"> </span><span class="k">as</span><span class="w"> </span><span class="o">*</span><span class="k">const</span><span class="w"> </span><span class="p">[</span><span class="kt">u8</span><span class="p">]</span><span class="w"> </span><span class="k">as</span><span class="w"> </span><span class="o">*</span><span class="k">const</span><span class="w"> </span><span class="p">[</span><span class="kt">u8</span><span class="p">;</span><span class="w"> </span><span class="mi">8</span><span class="p">]))</span><span class="w"> </span><span class="p">};</span><span class="w"></span>
<span class="w">   </span><span class="o">|</span><span class="w">                                           </span><span class="o">^^^^^^^^^^^^^^^</span><span class="w"></span>
</code></pre></div>

<p>for nightly toolchain</p>



<a name="212220002"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/split%20array%20to%20consecutive%20subarray%20%3F/near/212220002" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> lcnr <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/split.20array.20to.20consecutive.20subarray.20.3F.html#212220002">(Oct 04 2020 at 11:56)</a>:</h4>
<p>yeah, that's unfortunate</p>



<a name="212220015"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/split%20array%20to%20consecutive%20subarray%20%3F/near/212220015" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> lzutao <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/split.20array.20to.20consecutive.20subarray.20.3F.html#212220015">(Oct 04 2020 at 11:57)</a>:</h4>
<p>I guess we have to wait until arithmetic support for const generics arguments.</p>



<a name="212220060"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/split%20array%20to%20consecutive%20subarray%20%3F/near/212220060" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> lcnr <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/split.20array.20to.20consecutive.20subarray.20.3F.html#212220060">(Oct 04 2020 at 11:58)</a>:</h4>
<p>you mean <code>const_evaluatable_checked</code>?</p>



<a name="212220066"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/split%20array%20to%20consecutive%20subarray%20%3F/near/212220066" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> lcnr <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/split.20array.20to.20consecutive.20subarray.20.3F.html#212220066">(Oct 04 2020 at 11:58)</a>:</h4>
<p>I don't see how this will help though</p>



<a name="212220200"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/split%20array%20to%20consecutive%20subarray%20%3F/near/212220200" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> lzutao <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/split.20array.20to.20consecutive.20subarray.20.3F.html#212220200">(Oct 04 2020 at 12:03)</a>:</h4>
<p>The error in <a href="#narrow/stream/122651-general/topic/split.20array.20to.20consecutive.20subarray.20.3F/near/212219829">https://rust-lang.zulipchat.com/#narrow/stream/122651-general/topic/split.20array.20to.20consecutive.20subarray.20.3F/near/212219829</a> is const trait.<br>
What I mean for arithmetic is in <code>fn foo&lt;const N: usize&gt;(_: [u8; N]) -&gt; [u8; N - 3]</code>.</p>



<a name="212220260"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/split%20array%20to%20consecutive%20subarray%20%3F/near/212220260" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> lcnr <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/split.20array.20to.20consecutive.20subarray.20.3F.html#212220260">(Oct 04 2020 at 12:04)</a>:</h4>
<p>yeah, that's <code>const_evaluatable_checked</code>, which is both my child and still very broken</p>



<a name="212220271"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/split%20array%20to%20consecutive%20subarray%20%3F/near/212220271" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> lcnr <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/split.20array.20to.20consecutive.20subarray.20.3F.html#212220271">(Oct 04 2020 at 12:04)</a>:</h4>
<p>so <code>fn foo&lt;const N: usize&gt;(_: [u8; N]) -&gt; [u8; N - 3]</code> works if you enable <code>feature(const_evaluatable_checked)</code></p>



<a name="212221004"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/split%20array%20to%20consecutive%20subarray%20%3F/near/212221004" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> lzutao <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/split.20array.20to.20consecutive.20subarray.20.3F.html#212221004">(Oct 04 2020 at 12:24)</a>:</h4>
<p>that's nice.<br>
I implemented a function like this:</p>
<div class="codehilite" data-code-language="Rust"><pre><span></span><code><span class="k">const</span><span class="w"> </span><span class="k">fn</span> <span class="nf">split_bytearray</span><span class="o">&lt;</span><span class="k">const</span><span class="w"> </span><span class="n">PART</span>: <span class="kt">usize</span><span class="o">&gt;</span><span class="p">(</span><span class="n">arr</span>: <span class="p">[</span><span class="kt">u8</span><span class="p">;</span><span class="w"> </span><span class="mi">32</span><span class="p">])</span><span class="w"> </span>-&gt; <span class="p">[</span><span class="kt">u8</span><span class="p">;</span><span class="w"> </span><span class="mi">8</span><span class="p">]</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">    </span><span class="n">core</span>::<span class="n">assert</span><span class="o">!</span><span class="p">(</span><span class="n">PART</span><span class="w"> </span><span class="o">&lt;</span><span class="w"> </span><span class="mi">4</span><span class="p">);</span><span class="w"></span>
<span class="w">    </span><span class="k">const</span><span class="w"> </span><span class="n">START</span>: <span class="kt">usize</span> <span class="o">=</span><span class="w"> </span><span class="n">PART</span><span class="w"> </span><span class="o">*</span><span class="w"> </span><span class="mi">8</span><span class="p">;</span><span class="w"></span>
<span class="w">    </span><span class="k">const</span><span class="w"> </span><span class="n">END</span>: <span class="kt">usize</span> <span class="o">=</span><span class="w"> </span><span class="n">START</span><span class="w"> </span><span class="o">+</span><span class="w"> </span><span class="mi">8</span><span class="p">;</span><span class="w"></span>
<span class="w">    </span><span class="k">unsafe</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">        </span><span class="n">ptr</span>::<span class="n">read</span><span class="p">(</span><span class="o">&amp;</span><span class="n">arr</span><span class="p">[</span><span class="n">START</span><span class="o">..</span><span class="n">END</span><span class="p">]</span><span class="w"> </span><span class="k">as</span><span class="w"> </span><span class="o">*</span><span class="k">const</span><span class="w"> </span><span class="p">[</span><span class="kt">u8</span><span class="p">]</span><span class="w"> </span><span class="k">as</span><span class="w"> </span><span class="o">*</span><span class="k">const</span><span class="w"> </span><span class="p">[</span><span class="kt">u8</span><span class="p">;</span><span class="w"> </span><span class="mi">8</span><span class="p">])</span><span class="w"></span>
<span class="w">    </span><span class="p">}</span><span class="w"></span>
<span class="p">}</span><span class="w"></span>
</code></pre></div>

<p>but the <a href="https://play.rust-lang.org/?version=nightly&amp;mode=debug&amp;edition=2018&amp;gist=71e02509c3ee6fa880814a33d487e05b">compiler error</a>: <code>error[E0401]: can't use generic parameters from outer function</code></p>



<a name="212221023"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/split%20array%20to%20consecutive%20subarray%20%3F/near/212221023" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> lcnr <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/split.20array.20to.20consecutive.20subarray.20.3F.html#212221023">(Oct 04 2020 at 12:25)</a>:</h4>
<p>what's core::assert!</p>



<a name="212221027"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/split%20array%20to%20consecutive%20subarray%20%3F/near/212221027" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> lzutao <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/split.20array.20to.20consecutive.20subarray.20.3F.html#212221027">(Oct 04 2020 at 12:25)</a>:</h4>
<p>is that  allowed in design of <code>const_evaluatable_checked</code> ?</p>



<a name="212221035"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/split%20array%20to%20consecutive%20subarray%20%3F/near/212221035" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> lcnr <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/split.20array.20to.20consecutive.20subarray.20.3F.html#212221035">(Oct 04 2020 at 12:25)</a>:</h4>
<p>consts can't depend on outer generic params</p>



<a name="212221077"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/split%20array%20to%20consecutive%20subarray%20%3F/near/212221077" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> lcnr <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/split.20array.20to.20consecutive.20subarray.20.3F.html#212221077">(Oct 04 2020 at 12:26)</a>:</h4>
<p>so you probably want to use <code>PART * 8</code> directly here</p>



<a name="212221098"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/split%20array%20to%20consecutive%20subarray%20%3F/near/212221098" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> lcnr <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/split.20array.20to.20consecutive.20subarray.20.3F.html#212221098">(Oct 04 2020 at 12:26)</a>:</h4>
<p>instead of <code>assert!(PART &lt; 4)</code> you can use <code>where [u8; 3 - N]</code></p>



<a name="212221106"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/split%20array%20to%20consecutive%20subarray%20%3F/near/212221106" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> lcnr <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/split.20array.20to.20consecutive.20subarray.20.3F.html#212221106">(Oct 04 2020 at 12:27)</a>:</h4>
<p>let me try getting this to compile</p>



<a name="212221120"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/split%20array%20to%20consecutive%20subarray%20%3F/near/212221120" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> lzutao <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/split.20array.20to.20consecutive.20subarray.20.3F.html#212221120">(Oct 04 2020 at 12:27)</a>:</h4>
<p><span class="user-mention silent" data-user-id="216206">lcnr</span> <a href="#narrow/stream/122651-general/topic/split.20array.20to.20consecutive.20subarray.20.3F/near/212221023">said</a>:</p>
<blockquote>
<p>what's core::assert!</p>
</blockquote>
<p>I want to use libcore only.</p>



<a name="212221200"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/split%20array%20to%20consecutive%20subarray%20%3F/near/212221200" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> lcnr <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/split.20array.20to.20consecutive.20subarray.20.3F.html#212221200">(Oct 04 2020 at 12:29)</a>:</h4>
<p><span aria-label="thumbs up" class="emoji emoji-1f44d" role="img" title="thumbs up">:thumbs_up:</span></p>



<a name="212221289"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/split%20array%20to%20consecutive%20subarray%20%3F/near/212221289" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> lzutao <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/split.20array.20to.20consecutive.20subarray.20.3F.html#212221289">(Oct 04 2020 at 12:31)</a>:</h4>
<p><span class="user-mention silent" data-user-id="216206">lcnr</span> <a href="#narrow/stream/122651-general/topic/split.20array.20to.20consecutive.20subarray.20.3F/near/212221077">said</a>:</p>
<blockquote>
<p>so you probably want to use <code>PART * 8</code> directly here</p>
</blockquote>
<p>it works, but now <code>ptr::read</code> is not allowed in const fn</p>



<a name="212221355"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/split%20array%20to%20consecutive%20subarray%20%3F/near/212221355" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> lcnr <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/split.20array.20to.20consecutive.20subarray.20.3F.html#212221355">(Oct 04 2020 at 12:32)</a>:</h4>
<p>yeah</p>
<div class="codehilite" data-code-language="Rust"><pre><span></span><code><span class="cp">#![feature(const_generics, const_evaluatable_checked, const_panic)]</span><span class="w"></span>
<span class="k">use</span><span class="w"> </span><span class="n">core</span>::<span class="n">ptr</span><span class="p">;</span><span class="w"></span>

<span class="k">const</span><span class="w"> </span><span class="k">fn</span> <span class="nf">assert_less_than</span><span class="p">(</span><span class="n">l</span>: <span class="kt">usize</span><span class="p">,</span><span class="w"> </span><span class="n">r</span>: <span class="kt">usize</span><span class="p">)</span><span class="w"> </span>-&gt; <span class="kt">usize</span> <span class="p">{</span><span class="w"></span>
<span class="w">    </span><span class="n">assert</span><span class="o">!</span><span class="p">(</span><span class="n">l</span><span class="w"> </span><span class="o">&lt;</span><span class="w"> </span><span class="n">r</span><span class="p">);</span><span class="w"></span>
<span class="w">    </span><span class="mi">0</span><span class="w"></span>
<span class="p">}</span><span class="w"></span>

<span class="k">fn</span> <span class="nf">split_bytearray</span><span class="o">&lt;</span><span class="k">const</span><span class="w"> </span><span class="n">PART</span>: <span class="kt">usize</span><span class="o">&gt;</span><span class="p">(</span><span class="n">arr</span>: <span class="p">[</span><span class="kt">u8</span><span class="p">;</span><span class="w"> </span><span class="mi">32</span><span class="p">])</span><span class="w"> </span>-&gt; <span class="p">[</span><span class="kt">u8</span><span class="p">;</span><span class="w"> </span><span class="mi">8</span><span class="p">]</span><span class="w"></span>
<span class="k">where</span><span class="w"></span>
<span class="w">    </span><span class="p">[</span><span class="kt">u8</span><span class="p">;</span><span class="w"> </span><span class="n">assert_less_than</span><span class="p">(</span><span class="n">PART</span><span class="p">,</span><span class="w"> </span><span class="mi">4</span><span class="p">)]</span>: <span class="nb">Sized</span><span class="p">,</span><span class="w"></span>
<span class="p">{</span><span class="w"></span>
<span class="w">    </span><span class="k">unsafe</span><span class="w"> </span><span class="p">{</span><span class="w"> </span><span class="n">ptr</span>::<span class="n">read</span><span class="p">(</span><span class="o">&amp;</span><span class="n">arr</span><span class="p">[</span><span class="n">PART</span><span class="w"> </span><span class="o">*</span><span class="w"> </span><span class="mi">8</span><span class="o">..</span><span class="n">PART</span><span class="w"> </span><span class="o">*</span><span class="w"> </span><span class="mi">8</span><span class="w"> </span><span class="o">+</span><span class="w"> </span><span class="mi">8</span><span class="p">]</span><span class="w"> </span><span class="k">as</span><span class="w"> </span><span class="o">*</span><span class="k">const</span><span class="w"> </span><span class="p">[</span><span class="kt">u8</span><span class="p">]</span><span class="w"> </span><span class="k">as</span><span class="w"> </span><span class="o">*</span><span class="k">const</span><span class="w"> </span><span class="p">[</span><span class="kt">u8</span><span class="p">;</span><span class="w"> </span><span class="mi">8</span><span class="p">])</span><span class="w"> </span><span class="p">}</span><span class="w"></span>
<span class="p">}</span><span class="w"></span>

<span class="k">fn</span> <span class="nf">main</span><span class="p">()</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">    </span><span class="n">split_bytearray</span>::<span class="o">&lt;</span><span class="mi">3</span><span class="o">&gt;</span><span class="p">([</span><span class="mi">0</span><span class="p">;</span><span class="w"> </span><span class="mi">32</span><span class="p">]);</span><span class="w"></span>
<span class="w">    </span><span class="c1">// split_bytearray::&lt;4&gt;([0; 32]); // compilefail</span>
<span class="p">}</span><span class="w"></span>
</code></pre></div>



<a name="212221358"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/split%20array%20to%20consecutive%20subarray%20%3F/near/212221358" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> lcnr <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/split.20array.20to.20consecutive.20subarray.20.3F.html#212221358">(Oct 04 2020 at 12:32)</a>:</h4>
<p>but now it's not const</p>



<a name="212221378"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/split%20array%20to%20consecutive%20subarray%20%3F/near/212221378" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> lcnr <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/split.20array.20to.20consecutive.20subarray.20.3F.html#212221378">(Oct 04 2020 at 12:33)</a>:</h4>
<p>if you only need <code>[u8; 8]</code> you can build the array by manually indexing</p>



<a name="212221381"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/split%20array%20to%20consecutive%20subarray%20%3F/near/212221381" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> lcnr <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/split.20array.20to.20consecutive.20subarray.20.3F.html#212221381">(Oct 04 2020 at 12:33)</a>:</h4>
<p>but that kind of sucks</p>



<a name="212221426"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/split%20array%20to%20consecutive%20subarray%20%3F/near/212221426" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> lcnr <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/split.20array.20to.20consecutive.20subarray.20.3F.html#212221426">(Oct 04 2020 at 12:34)</a>:</h4>
<p>cc <span class="user-mention" data-user-id="124288">@oli</span> do we need const traits to get <code>arr[a..b]</code> to work in a const context?</p>



<a name="212221642"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/split%20array%20to%20consecutive%20subarray%20%3F/near/212221642" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Lukas Wirth <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/split.20array.20to.20consecutive.20subarray.20.3F.html#212221642">(Oct 04 2020 at 12:40)</a>:</h4>
<p>Is slicing here even required? You could just take a pointer to the first element you want no?</p>



<a name="212221651"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/split%20array%20to%20consecutive%20subarray%20%3F/near/212221651" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Lukas Wirth <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/split.20array.20to.20consecutive.20subarray.20.3F.html#212221651">(Oct 04 2020 at 12:41)</a>:</h4>
<p>something like <code>arr.as_ptr().add(PART)</code>, or am I missing something?</p>



<a name="212221713"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/split%20array%20to%20consecutive%20subarray%20%3F/near/212221713" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> lcnr <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/split.20array.20to.20consecutive.20subarray.20.3F.html#212221713">(Oct 04 2020 at 12:42)</a>:</h4>
<p>yeah, that would require pointer dereferences which are also not yet const afaik</p>



<a name="212221725"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/split%20array%20to%20consecutive%20subarray%20%3F/near/212221725" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> lcnr <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/split.20array.20to.20consecutive.20subarray.20.3F.html#212221725">(Oct 04 2020 at 12:42)</a>:</h4>
<p>or wait, we can always transmute using a union</p>



<a name="212221865"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/split%20array%20to%20consecutive%20subarray%20%3F/near/212221865" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> lzutao <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/split.20array.20to.20consecutive.20subarray.20.3F.html#212221865">(Oct 04 2020 at 12:46)</a>:</h4>
<p>yeah, I got it work: <a href="https://play.rust-lang.org/?version=nightly&amp;mode=debug&amp;edition=2018&amp;gist=b48984b7999f7ecc3ab1c6e61f09947f">https://play.rust-lang.org/?version=nightly&amp;mode=debug&amp;edition=2018&amp;gist=b48984b7999f7ecc3ab1c6e61f09947f</a></p>



<a name="212223105"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/split%20array%20to%20consecutive%20subarray%20%3F/near/212223105" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> oli <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/split.20array.20to.20consecutive.20subarray.20.3F.html#212223105">(Oct 04 2020 at 13:20)</a>:</h4>
<p>Slicing does need trait methods first. I currently don't have the time to work on that though. We do have some things implemented around this (impl const Trait is a thing), I don't remember what the exact status of all components is</p>



<a name="212239487"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/split%20array%20to%20consecutive%20subarray%20%3F/near/212239487" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> matt1992 <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/split.20array.20to.20consecutive.20subarray.20.3F.html#212239487">(Oct 04 2020 at 20:13)</a>:</h4>
<p>Couldn't <code>split_at</code> be made const  before traits can be const implemented?</p>



<hr><p>Last updated: Aug 07 2021 at 22:04 UTC</p>
</html>